package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sessions")
public class SessionController {
    /**
     * 用户登陆
     */
    @Autowired
    private IUserService userService;
    @PostMapping
    public JsonResult login(String email, String password){
        JsonResult result = new JsonResult();
        if (email != null && password!= null) {
            try {
                User user = userService.login(email,password);
                result.setResult(user);
            }catch (Exception e){
                e.getStackTrace();
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
    @DeleteMapping
    public void logout(){
        UserContext.setUser(null);
    }
}
